package com.example.springsecurity.service.impl;

import com.example.springsecurity.domain.RoProject;
import com.example.springsecurity.mapper.ExperimentMapper;
import com.example.springsecurity.mapper.ProjectTeachMapper;
import com.example.springsecurity.service.ProjectTeachService;
import com.example.springsecurity.utils.ResultBean;
import com.example.springsecurity.utils.ifnull.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName ProjectTeachServiceImpl
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-10-10 11:40
 **/
@Service
public class ProjectTeachServiceImpl implements ProjectTeachService {

    @Autowired
    private ProjectTeachMapper projectTeachMapper;

    @Autowired
    private ExperimentMapper experimentMapper;

    private Logger logger = LoggerFactory.getLogger(ProjectTeachServiceImpl.class);

    /**
     * 根据当前登录的用户(教师，负责人)获取其负责的实验项目
     * @param userId
     * @return
     */
    @Override
    public ResultBean getChargeProject(Long userId, String projectName, Integer pageNum, Integer pageSize) {
        /**
         * 首先根据用户id获取其负责的项目id
         */
        List<Integer> projectIds = projectTeachMapper.getProjectIdsByUserId(userId);
        List<RoProject> roProjects = null;
        /**
         * 分页
         */
        if(StringUtils.isNull(pageNum) || pageNum == 0){
            pageNum = 1;
        }
        if(StringUtils.isNull(pageSize) || pageSize == 0){
            pageSize = 10;
        }
        /**
         * 判断是否负责项目
         */
        if(StringUtils.isNull(projectIds) || projectIds.size() == 0){
            return ResultBean.success("没有负责的项目",null);
        }
        try{
            PageHelper.startPage(pageNum,pageSize);
            //获取负责的实验
            roProjects = experimentMapper.getProjects(projectIds,projectName);
            PageInfo<RoProject> pageInfo = new PageInfo<RoProject>(roProjects);
            return ResultBean.success("成功获取到内容",pageInfo);
        }catch (Exception e){
            logger.error("用户id为:{}的人,获取负责的实验失败",userId);
            return ResultBean.error("获取内容失败");
        }
    }
}
